package ah.szxy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 测试用Controller
 *
 * @author caohaiyang
 * @create 2020-09-11 下午 02:57
 */
@RestController
public class TestController {

    @GetMapping("/test")
    public ResponseEntity<String> test() {
        return ResponseEntity.ok("OK");
    }

    @GetMapping("/thread")
    public ResponseEntity<String> testThread() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        return ResponseEntity.ok("thread");
    }

    /**
     * 日志打印
     */
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    /**
     * 排队
     * @return
     */
    @GetMapping("/queue")
    public ResponseEntity<String> quence() {
        logger.info("开始处理 ....");
        return ResponseEntity.ok("quence");
    }

    /**
     * 降级
     */
    @GetMapping("/rt")
    public ResponseEntity<String> rt() throws InterruptedException {
        // 睡眠一秒
        TimeUnit.SECONDS.sleep(1);
        return ResponseEntity.ok("rt");
    }

    /**
     * 异常
     * @return
     */
    @GetMapping("/exception")
    public ResponseEntity<String> exception() {
        throw new RuntimeException("手动抛出异常");
    }

    @GetMapping("/buy")
    @SentinelResource("buy")
    public ResponseEntity<String>  bug( String goodsName, Integer goodsCount){
        return ResponseEntity.ok(String.format("购买%s， 一共%s件", goodsName, goodsCount));
    }
}